<template>
	<view class="personDetailWp">
		<person-card :userInfo='userInfo' :isFavorite='isFavorite' @handleMore='handleMore'
			@changIsFavorite='changIsFavorite'></person-card>
		<u-popup :show="showMoreListPopup" :round="10" @close='hiddenMoreListPopup' mode="bottom"
			:closeOnClickOverlay='true' closeable>
			<view class="moreItem">
				<view class="left">
					<image :showLoading="true" :src="userInfo.head" class="leftImg"></image>
				</view>
				<view class="right">
					<view>
						<text class="name"> {{userInfo.name}}</text> <text class="title"> {{userInfo.position}}</text>
					</view>
					<view class="company">
						{{userInfo.company}}
					</view>
				</view>
			</view>
			<!-- 操作栏 -->
			<view class="operateList">
				<view class="operateItem" v-for="(item, i) in operateList" :key="i" @click="handleOperateItem(item,i)">
					<view class="imgWp">
						<img :src="item.imgUrl" alt="">
					</view>
					<view class="text">
						{{item.text}}
					</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import baseConfig from '@/config/baseUrl.js'
	import personCard from '@/components/personCard/personCard.vue'
	export default {
		components: {
			personCard
		},
		data() {
			return {
				isFavorite: 0,
				userInfo: {},
				showMoreListPopup: false,
				operateList: [{
						imgUrl: 'https://file.casugn.com/storecompcard/temp/存通讯录@2x_1656472891228.png',
						text: '储存到通讯录'
					},
					{
						imgUrl: 'https://file.casugn.com/storecompcard/temp/邮箱@2x_1656472910550.png',
						text: '复制邮箱号码'
					},
					{
						imgUrl: 'https://file.casugn.com/storecompcard/temp/微信@2x_1656472898338.png',
						text: '添加微信'
					},
					{
						imgUrl: 'https://file.casugn.com/storecompcard/temp/位置 (2) 拷贝@2x_1656472904469.png',
						text: '去拜访'
					},
					{
						imgUrl: 'https://file.casugn.com/storecompcard/temp/删除@2x_1656472895045.png',
						text: '删除名片'
					},
				],
				showOperateListPopup: false,

			};
		},
		// 页面onLoad接受参数/ 查询接口获取userInfo
		// onLoad(option) {
		// 	const item = JSON.parse(decodeURIComponent(option.item))
		// 	  console.log(item,'页面onLoad接受参数');	
		// 	this.userInfo = item
		// 	this.isFavorite=this.userInfo.status //收藏状态
		// },
		onReady() {
			// //获取授权 
			// wx.getSetting({
			//   success(res) {
			//     if (!res.authSetting['scope.addPhoneContact']) {
			//       wx.authorize({
			//         scope: 'scope.addPhoneContact',
			//         success () {
			//         }
			//       })
			//     }
			//   }
			// })
		},
		onShow() {
			this.getPersonDetail()
		},
		methods: {
			//根据id 查询好友名片信息

			getPersonDetail() {
				let businessCardId = uni.getStorageSync('businessCardId')
				this.$api.getBusinessCard(businessCardId).then(res => {
					console.log(res)
					if (res.success) {
						this.userInfo = res.result
					}
				})
			},
			handleMore() {
				this.showMoreListPopup = true
			},
			hiddenMoreListPopup() {
				this.showMoreListPopup = false
			},
			//收藏操作
			async changIsFavorite(data) {
				this.isFavorite = data
				let res = await this.$api.updateCollect(this.userInfo.id, this.isFavorite)
				if (res.success) {
					//更新好友状态
					uni.showToast({
						title: res.message,
						icon: 'none'
					});
				}
			},
			//个人名片操作
			handleOperateItem(item, i) {
				//	console.log(this.userInfo);
				this.hiddenMoreListPopup()
				switch (i) {
					//存储到通讯录
					case 0:
						//	console.log('存储到通讯录')
						uni.addPhoneContact({
							nickName: this.userInfo.name,
							firstName: this.userInfo.name,
							remark: '备注',
							mobilePhoneNumber: this.userInfo.mobile,
							weChatNumber: this.userInfo.wechat,
							organization: this.userInfo.company, //公司
							title: this.userInfo.position, //职位
							workPhoneNumber: this.userInfo.workPhoneNumber ? this.userInfo.workPhoneNumber :
							'', //工作电话
							email: this.userInfo.mail ? this.userInfo.mail : '',
							url: this.userInfo.url ? this.userInfo.url : '',
							addressCountry: this.userInfo.addressCountry ? this.userInfo.addressCountry : '', //国家
							addressState: this.userInfo.addressState ? this.userInfo.addressState : '', //省份
							addressCity: this.userInfo.addressCity ? this.userInfo.addressCity : '', //城市
							addressStreet: this.userInfo.addressStreet ? this.userInfo.addressStreet : '', //街道
							addressPostalCode: this.userInfo.addressPostalCode ? this.userInfo.addressPostalCode :
								'', //邮政编码
							success: function() {
								console.log('success');
							},
							fail: function() {
								console.log('fail');
							}
						});
						break;
						//复制邮箱号码
					case 1:
						uni.setClipboardData({
							data: this.userInfo.mail,
							success: function() {
								console.log('已复制邮箱号码');
							}
						});
						break;
						//添加微信号	
					case 2:
						if (this.userInfo.weChatNumber) {
							uni.setClipboardData({
								data: this.userInfo.wechat,
								success: function() {
									console.log('已复制微信号');
								}
							});
						} else {
							uni.showToast({
								title: '用户未填写微信号',
								icon: 'none'
							});
						}

						break;
						//地图导航
					case 3:
						console.log('地图导航')
						let plugin = requirePlugin('routePlan');
						const key = baseConfig.mapData.mapKey; //使用在腾讯位置服务申请的key
						const referer = baseConfig.mapData.appName //调用插件的app的名称
						//模拟数据
						console.log(this.userInfo.coord)
						let endPoint = this.userInfo.coord
						if (endPoint.length < 1) {
							return uni.showToast({
								title: '用户未设置坐标信息',
								icon: 'none'
							});
						}
						wx.navigateTo({
							url: 'plugin://routePlan/index?key=' + key + '&referer=' + referer + '&endPoint=' +
								endPoint
						});

						break;
						//删除名片
					case 4:
						uni.showModal({
							title: '提示',
							content: '是否确认删除好友名片?',
							success: async (res) => {
								if (res.confirm) {
									let res = await this.$api.delBcCardBuddy(this.userInfo.id)
									console.log(res);
									if (res.success) {
										uni.showToast({
											title: '删除成功!',
											icon: 'none'
										});
										uni.navigateBack()
									}
								} else if (res.cancel) {
									console.log('用户点击取消');
								}
							}
						});

						break;
					default:
						break;
				}
			},
		}
	}
</script>

<style lang="scss">
	.personDetailWp {
		margin: 0 30upx;
		padding-top: 40upx;
	}

	.moreItem {
		display: flex;
		margin: 30upx;
		padding: 15upx 0;
		border-bottom: 1upx solid #eee;

		.left {
			padding-right: 30upx;

			.leftImg {
				width: 100upx;
				height: 100upx;
			}
		}

		.right {
			.name {
				width: 91upx;
				height: 29px;
				font-size: 30upx;
				font-weight: 500;
				color: #000000;
				line-height: 36upx;
			}

			.title {
				padding-left: 22upx;
				font-size: 18upx;
				font-weight: 400;
				color: #666666;
				line-height: 36upx;
			}

			.company {
				padding-top: 22upx;
				font-size: 24upx;
				font-weight: 400;
				color: #333333;
				line-height: 36upx;
			}
		}
	}

	.operateList {
		margin-left: 60upx;
		margin-bottom: 60upx;

		.operateItem {
			display: flex;
			padding: 25upx 0;

			.imgWp {
				margin-right: 30upx;
				width: 37upx;
				height: 37upx;

				img {
					width: 37upx;
					height: 37upx;
				}
			}

			.text {

				font-size: 30upx;
				font-weight: 400;
				color: #333333;
				line-height: 36upx;
			}
		}
	}
</style>
